﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using LPV_POS.mh_baocao;
using LPV_POS.Baocao;
using LPV_POS.Baocao.Ellic;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos
{
    public partial class mh_danhsachtrahang : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        xulyngaythangnam ngaythang;
        thuvien tv;
        DataTable tb;
        DataSet dset;
        string[] s;
        object[] o;
       
        #region "Muti language"
        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                label9.Text = rm.GetString("khohang", culture);
                label2.Text = rm.GetString("mon", culture);
                label3.Text = rm.GetString("tungay", culture);
                label1.Text = rm.GetString("denngay", culture);
                bton_clr.Text = rm.GetString("clr", culture);
                check_tungay.Text = rm.GetString("tim", culture);
                bton_xoatatca.Text = rm.GetString("xoatatca", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_in.Text = rm.GetString("xuatexcel", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                this.col_khohang.Caption = rm.GetString("khohang", culture);
                this.col_tenhanghoa.Caption = rm.GetString("mon", culture);
                this.col_soluong.Caption = rm.GetString("soluong", culture);
                this.col_DVT.Caption = rm.GetString("dvt", culture);
                this.Text = rm.GetString("danhsachtralai", culture);
               
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        // select danh sach hang hoa tra lai
        private DataTable f_selectdshhtralai()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };

            tb = tv.SP_SelectDataTable("sp_mhdanhsachtralai", s, o);
            //try
            //{
            //    DataRow dw = tb.NewRow();
            //    dw[0] = "";
            //    dw[1] = "";
            //    dw[2] = ""; 
            //    dw[3] = "Tổng tiền";
            //    dw[4] = "";
            //    dw[5] = 0;
            //    dw[6] = 0;
            //    dw[7] = 10000;
            //    dw[8] = "";
            //    dw[9] = "";
            //    tb.Rows.InsertAt(dw,tb.Rows.Count + 1);
            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show(ex.ToString(),"");
            //}
           
            //tb.Columns.Add("STT", typeof(int));
            //for (int i = 0; i < tb.Rows.Count; i++)
            //    tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp = tb.DefaultView;
            //dv_temp.Sort = "STT DESC";
            //return dv_temp.ToTable();
            return tb;
        }
        // Tim hang hoa tra lai khi nhan nut tim
        private DataTable f_selectfindhhtralai(string tenhh, string tungay, string denngay, string khohang)
        {
            tv = new thuvien();
            s = new string[] { "@FLAG", "@CHOOSE", "@TENHH", "@TUNGAY_STR", "@DENNGAY_STR","@KHOHANG" };
            o = new object[] { 1, 2,tenhh, tungay, denngay,khohang };
            tb = tv.SP_SelectDataTable("sp_mhdanhsachtralai", s, o);
            return tb;
        }
        private DataTable f_findnhomhh()
        {
            DataTable tb;
            ngaythang = new xulyngaythangnam();
            string tenhanghoa;
            string tungay;
            string denngay;
            string khohang;
           
            if (txt_tenhanghoa.Text != "")
                tenhanghoa = "'%" + txt_tenhanghoa.Text + "%'";
            else
                tenhanghoa = "";

           // if (date_tungay.Text != "" && date_denngay.Text !="")
            if (check_ngay.Checked == true)
            {
                tungay = ngaythang.chuyenngay(date_tungay.Text);
                denngay = ngaythang.chuyenngay(date_denngay.Text);
            }
            else
            {
                tungay = "";
                denngay = "";
            }

            if (com_khohang.Text != "--- Chọn kho hang ---")
                khohang = com_khohang._GetselectedID();
            else
                khohang = "";

            tb = f_selectfindhhtralai(tenhanghoa, tungay, denngay, khohang);
            //tb.Columns.Add("STT", typeof(int));
            //for (int i = 0; i < tb.Rows.Count; i++)
            //    tb.Rows[i]["STT"] = i + 1;
            return tb;

        }

        // select danh sach kho hang
        private DataTable f_selectdskhohangforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 3 };
            tb = tv.SP_SelectDataTable("sp_mhdanhsachtralai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn kho hàng ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // xoa hang hoa tra lai
        private int f_deletehanghoatralai(int maphieu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 2, 1, maphieu};
            int ketqua = tv.SP_Update("sp_mhdanhsachtralai", s, o);
            return ketqua;
        }

        // xoa tat ca hang hoa tra lai
        private int f_deletetatcahanghoatralai()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose"};
            o = new object[] { 2, 2};
            int ketqua = tv.SP_Update("sp_mhdanhsachtralai", s, o);
            return ketqua;
        }
        public mh_danhsachtrahang()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            tv = new thuvien();
            tv.f_showdataoncombox(com_khohang, f_selectdskhohangforcom(), "OBJID", "OBJNAME");
        }

        private void mh_danhsachtrahang_Load(object sender, EventArgs e)
        {

            DataTable tb = f_selectdshhtralai();
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            luoi_dshangtralai.MainView = grid_dshangtralai;
            luoi_dshangtralai.DataSource = tb;
            date_tungay.DateTime = DateTime.Today;
            date_denngay.DateTime = DateTime.Today;
            com_khohang.Text = "--- Chọn kho hàng ---";

            if (tb.Rows.Count <= 0)
                return;
            txt_tongtien.Text = tb.Compute("Sum(THANHTIEN)", "THANHTIEN >= 0").ToString();
            txt_tongtien.Text = int.Parse(txt_tongtien.Text).ToString("N1");
            
        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            //if (check_ngay.Checked == true)
            //{
            //    date_tungay.DateTime= DateTime.Today;
            //    date_denngay.DateTime = DateTime.Today;
            //}
            //else
            //{
            //    date_tungay.Text = "";
            //    date_denngay.Text = "";
               
            //}
        }

        private void check_tungay_Click(object sender, EventArgs e)
        {
            luoi_dshangtralai.MainView = grid_dshangtralai;
            luoi_dshangtralai.DataSource = f_findnhomhh();
            
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_dshangtralai.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string maphieu = grid_dshangtralai.GetRowCellValue(grid_dshangtralai.FocusedRowHandle, col_khohang).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletehanghoatralai(Convert.ToInt32(maphieu));
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lại khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_dshangtralai.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_dshangtralai.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_xoatatca_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_dshangtralai.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa tất cả hàng hóa trả lại?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletetatcahanghoatralai();
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                luoi_dshangtralai.DataSource = null;
            }
        }
        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

            this.Close();
            //}
        }

        private void bton_in_Click(object sender, EventArgs e)
        {
          
        //    tb = f_selectdshhtralai();
         
           
            SaveFileDialog s = new SaveFileDialog();
            s.Filter += "*.xls|*.xls";
            s.FileName = "HangHoaTraLai" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".xls";
            if (s.ShowDialog() == DialogResult.OK)
            {
                string FN = s.FileName;
                grid_dshangtralai.ExportToExcelOld(FN);
                //grid_dshangtralai.ExportToExcelOld(@"D:\\tralai.xls");
                if (System.IO.File.Exists(s.FileName))
                    System.Diagnostics.Process.Start(s.FileName);
            }

 
            //tb = (DataTable)luoi_dshangtralai.DataSource;
            //if (tb.Rows.Count <= 0)
            //{
            //    Mod_messagebox mess = new Mod_messagebox();
            //    mess.messagebox_pos("Thông báo", "Không có dữ liệu", loaithongbao.thongtin.GetHashCode());
            //    return;
            //}
            //dset = new DataSet();
            //dset.Tables.Add(tb);

            //if (date_tungay.Text == "")
            //{
            //    Variable.str_tungay = "";
            //}
            //else
            //{
               
            //      Variable.str_tungay = "Từ ngày " + date_tungay.Text + " đến ngày " + date_denngay.Text;
            //}
            //if(txt_tenhanghoa.Text =="")
            // Variable.str_tenhanghoa = "Tất cả hàng hóa";
            //else
            //Variable.str_tenhanghoa = tb.Rows[0]["TENHH"]+"";


            //bc_hanghoatralai rpt_hanghoatralai = new bc_hanghoatralai();
            //mh_bc_hanghoatralai frm_bc_hanghoatralai = new mh_bc_hanghoatralai(dset,rpt_hanghoatralai);

          //  frm_bc_hanghoatralai.Show();
        }

        private void bton_clr_Click(object sender, EventArgs e)
        {
            com_khohang._SetselectedID("1");
         //   check_ngay.Checked = false;
            txt_tenhanghoa.Text = "";
            com_khohang.Focus();

        }

        private void bton_tenhanghoa_Click(object sender, EventArgs e)
        {
            txt_tenhanghoa.Text = new cpublic().loadformchu_point(250, 200);
        }

        private void grid_dshangtralai_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                int stt = 1 + e.RowHandle;
                e.Info.DisplayText = stt.ToString();
            }
        }

      
    }
}